/*
 *      calendar.js
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


krugLet=0;   //круг Лет
krugLav=0;   //круг Жизни
SMZH=0       //лет от СМЗХ
may="";      //месяц
dat=0 ;      //число
day="";      //день недели

//вычисляет разницу дней между датами d1-d2
function CalkDay(d1,m1,y1,d2,m2,y2) {
 var dow = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
 var nd1=d1; var nd2=d2;
 //дней до нового гада в обоих гадах
 while (m1>1) {
   m1--; nd1+=dow[m1-1]; if (m1==2) nd1+=VYear(y1);
              }
 while (m2>1) {
   m2--; nd2+=dow[m2-1]; if (m2==2) nd2+=VYear(y2);
              }
 var nd=nd1-nd2;
 //дней в промеж. годах
 while (y1>y2) {
   y1--; nd+=365+VYear(y1);
              }
 return nd;
}
//вычисляет год високосный или нет
function VYear(y) {
 if ((y%1000)==0||((y%4)==0&&(y%100)!=0)) {return 1} else {return 0}
}

//григориансое исчисление
  Date.prototype.getActualMonth = getActualMonth;
  Date.prototype.getActualDay   = getActualDay;
  Date.prototype.getCalendarDay = getCalendarDay;
  Date.prototype.getCalendarMonth = getCalendarMonth;

  function getActualMonth() {
    var n = this.getMonth();
    n += 1;
    return n;
  }

  function getActualDay() {
    var n = this.getDay();
    n += 1;
    return n;
  }

  function getCalendarDay() {
    var n = this.getDay();
    var dow = new Array("Воскресенье","Понедельник","Вторник","Среда","Четверг","Пятница","Суббота");
    return dow[n];
  }

  function getCalendarMonth() {
    var n = this.getMonth();
    var moy = new Array("Января","Февраля","Марта","Апреля","Мая","Июня","Июля","Августа","Сентября","Октября","Ноября","Декабря");
    return moy[n];
  }

//древнеее исчисление
  function Kolayda(d,m,g,h) {
    var Am = 0;
    if ((h>18)||(h>17&&m>3&&m<10)) d++; //коррекция Числа вечером
   //Круг Лета
    krugLet = Math.floor( (g+4)%16 );
    if (krugLet == 0) krugLet=16; //Круг Лета верен до осеннего равнодействия
    if (m>9) g++;
    if (m==9) if ( (d>21)||(d>20&&krugLet>4)||(d>19&&krugLet>8)||(d>18&&krugLet>12) ) g++;
    krugLet = Math.floor( (g+4)%16 );
    if (krugLet == 0) krugLet=16;
   //круг Жизни
    krugLav = Math.floor( (g+4)%144 );
   //лето от смзх
    SMZH = g+5508;
   //месяц
    if (m==1) {if ( (d<20)||(d<23&&krugLet<5)||(d<22&&krugLet<9)||(d<21&&krugLet<13)||(d<21&&krugLet==16) ) {Am=3} else {Am=4}};
    if (m==2) {Am = 4};
    if (m==3) {if ( (d<1) ||(d<4&&krugLet<4) ||(d<3&&krugLet<8) ||(d<2&&krugLet<12) ||(d<1&&krugLet<16)||(d<2&&krugLet==16) ) {Am=4} else {Am=5}};
    if (m==4) {if ( (d<11)||(d<14&&krugLet<4)||(d<13&&krugLet<8)||(d<12&&krugLet<12)||(d<11&&krugLet<16)||(d<12&&krugLet==16) ) {Am=5} else {Am=6}};
    if (m==5) {if ( (d<21)||(d<24&&krugLet<4)||(d<23&&krugLet<8)||(d<22&&krugLet<12)||(d<21&&krugLet<16)||(d<23&&krugLet==16) ) {Am=6} else {Am=7} };
    if (m==6) {Am = 7};
    if (m==7) {if ( (d<1) ||(d<4&&krugLet<4) ||(d<3&&krugLet<8) ||(d<2&&krugLet<12) ||(d<1&&krugLet<16) ||(d<3&&krugLet==16) ) {Am=7} else {Am=8}};
    if (m==8) {if ( (d<10)||(d<13&&krugLet<4)||(d<12&&krugLet<8)||(d<11&&krugLet<12)||(d<10&&krugLet<16)||(d<13&&krugLet==16) ) {Am=8} else {Am=9}};
    if (m==9) {if ( (d<20)||(d<23&&krugLet<5)||(d<22&&krugLet<9)||(d<21&&krugLet<13) ) {Am=9} else Am=1};
    if (m==10) {if (d>30 && krugLet>12)  {Am=2} else {Am=1}};
    if (m==11) {if ( (d<3&&krugLet<4)||(d<2&&krugLet<8)||(d==1) ) {Am=1} else {Am=2}};
    if (m==12) {if ( (d<10)||(d<13&&krugLet<5)||(d<12&&krugLet<9)||(d<11&&krugLet<13)||(d<10&&krugLet<16) ||(d<11&&krugLet==16) ) {Am=2} else {Am=3}};
    var moy = new Array("Рамхатъ","Айлетъ","Бейлетъ","Гейлетъ","Дайлетъ","Элетъ","Вэйлетъ","Хейлетъ", "Тайлетъ");
    may = moy[Am-1];
   //число
    var x;
    var md1 = new Array(23,23,22,22,21,21,20,20,  9);
    var md2 = new Array(03,03,02,02,01,01,31,31, 11);
    var md3 = new Array(13,13,12,12,11,11,10,11, 12);
    var md4 = new Array(23,23,22,22,21,21,20,21,  1);
    var md5 = new Array(04,03,03,02,02,01,01,02,  3);
    var md6 = new Array(14,13,13,12,12,11,11,12,  4);
    var md7 = new Array(24,23,23,22,22,21,21,23,  5);
    var md8 = new Array(04,03,03,02,02,01,01,03,  7);
    var md9 = new Array(13,12,12,11,11,10,10,13,  8);
    var mdd = new Array(md1,md2,md3,md4,md5,md6,md7,md8,md9);
    var kd  = new Array(0,0,0,1,2,2,2,3,4,4,4,5,6,6,6,7);
    x = mdd[Am-1];
    dat = x[kd[krugLet-1]]; //начало месяца
    var mx = x[8];
    var gx = g;
    if (Am==2&&dat==31) mx=10;
    if (Am==3&&m==1) gx ++;
    dat = CalkDay(d,m,gx,dat,mx,g)+1;
   //день недели
    md1 = new Array(1,  10,  20,  29,  39,  48,  49,  58,  68,  77,  87,  96,  97, 106, 116, 125, 135, 144);
    md2 = new Array(3,  12,  22,  31,  41,  51,  60,  70,  79,  89,  99, 108, 118, 127, 137 );
    md3 = new Array(5,  14,  24,  34,  43,  53,  62,  72,  82,  91, 101, 110, 120, 130, 139);
    md4 = new Array(7,  16,  17,  26,  36,  45,  55,  64,  65,  74,  84,  93, 103, 112, 113, 122, 132, 141);
    md5 = new Array(9,  19,  28,  38,  47,  57,  67,  76,  86,  95, 105, 115, 124, 134, 143);
    md6 = new Array(2,  11,  21,  30,  40,  50,  59,  69,  78,  88,  98, 107, 117, 126, 136);
    md7 = new Array(4,  13,  23,  32,  33,  42,  52,  61,  71,  80,  81,  90, 100, 109, 119, 128, 129, 138);
    md8 = new Array(6,  15,  25,  35,  44,  54,  63,  73,  83,  92, 102, 111, 121, 131, 140);
    md9 = new Array(8,  18,  27,  37,  46,  56,  66,  75,  85,  94, 104, 114, 123, 133, 142);
    mdd = new Array(md1,md2,md3,md4,md5,md6,md7,md8,md9);
    var n1=0; //в этот день начинается лето
    var l;
    for (var i=0;i<9;i++) {x=mdd[i];l=x.length; ;for (var j=0;j<l;j++) {
      if (krugLav==x[j]) {n1=i+1; break;}
      }};

    if (krugLet==16) {
    md1 = new Array(1,6,2,7,3,8,4,9,5);
    md4 = new Array(4,9,5,1,6,2,7,3,8);
    md7 = new Array(7,3,8,4,9,5,1,6,2);
                      }
    else {
    md1 = new Array(1,6,1,6,1,6,1,6,1);
    md4 = new Array(4,9,4,9,4,9,4,9,4);
    md7 = new Array(7,3,7,3,7,3,7,3,7);
         }
    md2 = new Array(2,7,2,7,2,7,2,7,2);
    md3 = new Array(3,8,3,8,3,8,3,8,3);
    md5 = new Array(5,1,5,1,5,1,5,1,5);
    md6 = new Array(6,2,6,2,6,2,6,2,6);
    md8 = new Array(8,4,8,4,8,4,8,4,8);
    md9 = new Array(9,5,9,5,9,5,9,5,9);
    mdd = new Array(md1,md2,md3,md4,md5,md6,md7,md8,md9);
    x=mdd[n1-1];
    l=x[Am-1]; //в этот день начинается месяц
den-rojd.js:146Uncaught TypeError: Cannot read property '0' of undefined
    x=dat%9;
    l+=x-1; l=l%9; if (l==0) l=9;
    mdd = new Array("Понедельник","Вторник", "Тритейник", "Четверик", "Пятница", "Шестица", "Седьмица", "Осьмица", "Неделя");
    day=mdd[l-1];
}


//***
function KolaydaToday(){
  Kolayda(today.getDate(),today.getActualMonth(),today.getFullYear(),today.getHours())
}
function getKalendar(){
var dd = document.form1.dd.options[document.form1.dd.options.selectedIndex].text;
var mm = document.form1.mm.options[document.form1.mm.options.selectedIndex].text;
var yy = document.form1.yy.value;
var zz = document.form1.zz.options[document.form1.zz.options.selectedIndex].text;
var hh = document.form1.zz.options.selectedIndex;
if (hh==0) {hh=16} else {hh=20};
var txt="<font size=2>Расчет на: "+dd+"."+mm+"."+yy+"г. - "+zz;
Kolayda(eval(dd),eval(mm),eval(yy),hh);
txt += "<hr><center><b>"+day+", "+dat+"</b> число месяца <b>"+may+" "+ SMZH + "</b> Лета от С.М.З.Х.<br>Лето в круге Лет: <b>"+krugLet+"</b> <A HREF='#ДКЧ' >Лето в круге Жизни</a>: <b>"+krugLav+"</b></font></center>";
document.all.resultat.innerHTML = txt; //записьв id="resultat"
}

//***
today = new Date();
KolaydaToday();
